package com.example.warehouse.bus.controller;

import com.example.warehouse.bus.pojo.param.BusInportAddNewParam;
import com.example.warehouse.bus.pojo.param.BusInportUpdateParam;
import com.example.warehouse.bus.pojo.vo.BusInportListItemVO;
import com.example.warehouse.bus.service.IBusInportService;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.security.CurrentPrincipal;
import com.example.warehouse.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 进货管理的控制类
 *
 * @author XXH
 */
@Slf4j
@RestController
@RequestMapping("/inport")
@Api(tags = "0.1 进货管理")
@Validated
public class BusInportController {

    @Autowired
    private IBusInportService inportService;

    public BusInportController() {
        log.info("创建控制器对象:BusInportController");
    }

    @ApiOperation("添加进货")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/bus/inport/add-new')")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody BusInportAddNewParam busInportAddNewParam, @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理[添加进货]业务,位置:controller,参数:{}", busInportAddNewParam);
        inportService.insert(busInportAddNewParam,currentPrincipal );
        return JsonResult.ok();
    }

    @ApiOperation("删除进货")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/bus/inport/delete')")
    @PostMapping("/delete")
    public JsonResult deleteById(@RequestBody Long id,@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理[根据Id删除进货数据]业务,位置:controller,参数:id{},当事人:{}", id,currentPrincipal);
        inportService.deleteById(id,currentPrincipal);
        return JsonResult.ok();
    }

    @ApiOperation("修改进货")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/bus/inport/update')")
    @PostMapping("/update")
    public JsonResult updateById(BusInportUpdateParam busInportUpdateParam) {
        log.debug("开始处理[根据Id修改进货数据]业务,位置:controller,参数:{}", busInportUpdateParam);
        inportService.updateById(busInportUpdateParam);
        return JsonResult.ok();

    }


//    /**
//     * 查询列表的第一个方法,使用了默认的页数,只传了一个pageNum参数
//     * @param page
//     * @return
//     */
//    @ApiOperation("查询进货列表")
//    @ApiOperationSupport(order = 421)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "page", value = "页码", dataType = "int")
//    })
//    @GetMapping("")
//    public JsonResult list(Integer page) {
//        log.debug("开始处理【查询进货列表】请求，页码：{}", page);
//        Integer pageNum = page == null ? 1 : page;
//        PageData<BusInportListItemVO> pageData = inportService.list(pageNum);
//        return JsonResult.ok(pageData);
//    }


    /**
     * 查询列表的第二个方法,用户主动传入
     * @param page
     * @return
     */
    @ApiOperation("查询进货列表")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/bus/inport/select')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据数目,不填默认为5页", dataType = "int")

    })
    @GetMapping("/select")
    public JsonResult list(Integer page,Integer pageSize) {
        log.debug("开始处理【查询进货列表】请求，页码：{},每页条目数{}", page,pageSize);
        Integer pageNum = page == null ? 1 : page;
       PageData<BusInportListItemVO> pageData;
       if (pageSize==null){
            pageData = inportService.list(pageNum);
       }else{
           pageData = inportService.list(pageNum,pageSize);
       }
        return JsonResult.ok(pageData);
    }

}
